<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-09-22 10:06:31
 * @LastEditors: Sam
 * @LastEditTime: 2022-10-28 13:30:50
 */
namespace app\api\controller;
use app\admin\model\{StoreCouponIssue as CouponModel,StoreCouponUser};
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("优惠券")
 */
class Coupon extends ApiBase {


    /**
     * @Apidoc\Title("可用优惠券列表")
     * @Apidoc\Url("/api/coupon_list")
     * @Apidoc\Method("GET")
     * @Apidoc\Param("type", type="int",require=true, desc="类型 0不限制 1 品类券")
     * @Apidoc\Param("page", type="string",require=true, desc="页面")
     * @Apidoc\Param("limit", type="string",require=true, desc="数量")
     * @Apidoc\Param("product_id", type="int",desc="产品id" )
     * @Apidoc\Returned("[]", type="list[]", desc="列表",
     *      @Apidoc\Returned("id", type="int",desc="优惠券id"),
     *      @Apidoc\Returned("start_time", type="int",desc="优惠券开始时间"),
     *      @Apidoc\Returned("end_time", type="int",desc="优惠券结束时间"),
     *      @Apidoc\Returned("coupon_price", type="int",desc="优惠券面值"),
     *      @Apidoc\Returned("use_min_price", type="int",desc="优惠券最低消费"),
     *      @Apidoc\Returned("title", type="string",desc="优惠券名称"),
     *      @Apidoc\Returned("is_use", type="int",desc="是否已拥有 0 未领取 1已领取"),
     * )
     */
   public function list(){
        $uid = $this->get_user_id();
        $type = request()->param('type/d',0);
        $page = request()->param('page/d',1);
        $limit = request()->param('limit/d',10);
        $product_id = request()->param('product_id/d',0);
        \json_response(1,'success',CouponModel::getCouponList($uid, $limit, $page,$type,$product_id));
   }



    /**
     * @Apidoc\Title("领取优惠券")
     * @Apidoc\Url("/api/receive_coupon")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("couponId", type="int",require=true, desc="优惠券id")
     */
    public function receive()
    {
        $couponId = param_check('couponId');
        if (!$couponId || !is_numeric($couponId)) json_response(0,'参数错误!');
        if (CouponModel::issueUserCoupon($couponId, $this->get_user_id())) {
            json_response(1,'领取成功');
        } else {
            json_response(0,CouponModel::getErrorInfo('领取失败!'));
        }
    }

     /**
     * @Apidoc\Title("用户优惠券")
     * @Apidoc\Url("/api/user_coupon")
     * @Apidoc\Method("GET")
     * @Apidoc\Param("type", type="int",require=true, desc="类型 0 全部 1未使用 2已使用 3已过期")
     * @Apidoc\Returned("[]", type="list[]", desc="列表",
     *      @Apidoc\Returned("id", type="int", desc="用户优惠券id"),
     *      @Apidoc\Returned("coupon_title", type="string", desc="优惠券标题"),
     *      @Apidoc\Returned("coupon_price", type="string", desc="优惠券面值"),
     *      @Apidoc\Returned("use_min_price", type="string", desc="最低消费"),
     *      @Apidoc\Returned("status", type="int", desc="0：未使用，1：已使用, 2:已过期"),
     *      @Apidoc\Returned("applicable_type", type="int", desc="0：通用，1：品类券, 2:产品券"),
     *      @Apidoc\Returned("_add_time", type="string", desc="获得时间"),
     *      @Apidoc\Returned("_end_time", type="string", desc="结束使用时间"),
     * )
     */
    public function user_coupon()
    {
        $types = request()->param('type/d',0);
        switch ($types) {
            case 0:
            case '':
                $list = StoreCouponUser::getUserAllCoupon($this->get_user_id()); //用户全部优惠券
                break;
            case 1:
                $list = StoreCouponUser::getUserValidCoupon($this->get_user_id()); //未使用的优惠券
                break;
            case 2:
                $list = StoreCouponUser::getUserAlreadyUsedCoupon($this->get_user_id()); //已使用的优惠券
                break;
            default: 
                $list = StoreCouponUser::getUserBeOverdueCoupon($this->get_user_id()); //已过期优惠券
                break;
        }

        json_response(1,'success',$list);
    }
    
}